/**
 * 主要功能
 * 1.获取初始信息，从后台获取区域和分类信息，填充到前台
 * 1-2 验证表单的输入
 * 2.提交数据到server
 * 3.返回处理结果
 */
$(function () {
    var shopId = getQueryString("shopId");//从url获取shopId
    var isEdit = shopId ? true : false; //有shopId就是更新，没有就是注册
    var initUrl = '/o2o/shopadmin/getshopinitinfo';//初始化后台地址
    var registerShopUrl = '/o2o/shopadmin/registershop'; //后台注册商铺地址
    var shopInfoUrl = '/o2o/shopadmin/getshopbyid?shopId=' + shopId;//根据店铺id获取店铺信息
    var editShopUrl = '/o2o/shopadmin/modifyshop'; //编辑店铺信息url
    // 初始化加载
    if (!isEdit) {
        getShopInitInfo();
    } else {
        getShopInfo(shopId);
    }

    //修改店铺信息 根据id获取shop信息
    function getShopInfo(shopId) {
        $.getJSON(shopInfoUrl, function (data) {
            if (data.success) {
                var shop = data.shop;
                $('#shop-name').val(shop.shopName);
                $('#shop-addr').val(shop.shopAddr);
                $('#shop-phone').val(shop.phone);
                $('#shop-desc').val(shop.shopDesc);
                var shopCategory = '<option data-id="'
                    + shop.shopCategory.shopCategoryId + '" selected>'
                    + shop.shopCategory.shopCategoryName + '</option>';
                var tempAreaHtml = '';
                data.areaList.map(function (item, index) {
                    tempAreaHtml += '<option data-id="' + item.areaId + '">'
                        + item.areaName + '</option>';
                })
                $('#shop-category').html(shopCategory);
                $('#shop-category').attr('disabled', 'disabled');
                $('#area').html(tempAreaHtml);
                $("#area option[data-id='" + shop.area.areaId + "']").attr("selected", "selected");
            }
        });
    }

    //注册店铺 从后台获取区域信息和店铺分类信息，填入到前台页面
    function getShopInitInfo() {
        console.log("初始化数据!")
        $.getJSON(initUrl, function (data) {
            if (data.success) {
                var tempHtml = '';
                var tempAreaHtml = '';
                var shopCategoryList = data.shopCategoryList;
                shopCategoryList.map(function (item, index) {
                    tempHtml += '<option data-id="' + item.shopCategoryId + '">' + item.shopCategoryName + '</option>';
                });
                var areaList = data.areaList;
                areaList.map(function (item, index) {
                    tempAreaHtml += '<option data-id="' + item.areaId + '">' + item.areaName + '</option>';
                });
                $('#shop-category').html(tempHtml);
                $('#area').html(tempAreaHtml);
            }
        });
    }

    //点击提交获取输入表单的内容
    $("#submit").click(function () {
        var shop = {};
        if (isEdit) {
            shop.shopId = shopId;
        }
        shop.shopName = $('#shop-name').val();
        shop.shopAddr = $('#shop-addr').val();
        shop.phone = $('#shop-phone').val();
        shop.shopDesc = $('#shop-desc').val();
        //店铺分类
        shop.shopCategory = {
            shopCategoryId: $('#shop-category').find('option').not(function () {
                return !this.selected;
            }).data("id")
        };
        //店铺区域
        shop.area = {
            areaId: $('#area').find('option').not(function () {
                return !this.selected;
            }).data("id")
        };
       //获取上传文件输入流
        var shopImg = $('#shop-img')[0].files[0];

        var formData = new FormData()
        formData.append("shopImg", shopImg);
        formData.append("shopStr", JSON.stringify(shop));
        /*获取验证码*/
        var verifyCodeActual = $('#j_captcha').val();

        if (!verifyCodeActual) {
            $.toast('请输入验证码！');
            return;
        }
        formData.append("verifyCodeActual", verifyCodeActual);

        //提交后台, 如果编辑就提交到编辑的url,如果不是就提交到注册的url
        $.ajax({
            url: (isEdit ? editShopUrl : registerShopUrl),
            type: 'POST',
            data: formData,
            contentType: false,
            processData: false,
            cache: false,
            success: function (data) {
                if (data.success) {
                    $.toast('提交成功!');
                    $('#captcha_img').click();
                } else {
                    /*不管成功还是失败都更新一下验证码图片*/
                    $.toast('提交失败!' + data.errMsg);
                    $('#captcha_img').click();
                }
            }
        })

    })
})
